package com.visikard.asynctask;

import java.util.List;

import android.os.Handler;
import android.os.Message;

import com.visikard.application.UIMessage;
import com.visikard.business.GiftKardBUS;
import com.visikard.item.ItemGiftKardRedeem;
import com.visikard.response.BaseResponse;
import com.visikard.response.GetGiftKardResponse;

public class GetGiftKardTask extends BaseTask {

	public static final int POS_MERCHANT_ID = 0;
	public static final int POS_REDEEM_CODE = 1;

	public GetGiftKardTask(Handler handler) {
		super(handler);
	}

	public GetGiftKardTask(Handler handler, boolean isShowProgress) {
		super(handler, isShowProgress);
	}

	@Override
	protected Object doBackgroundWork(Object... params) {

		// Get params
		long merchantId = (Long) params[POS_MERCHANT_ID];
		String redeemCode = (String) params[POS_REDEEM_CODE];

		// Validation

		// Call service
		GiftKardBUS busObj = new GiftKardBUS();
		return busObj.getGiftKardList(merchantId, redeemCode);
	}

	@Override
	protected boolean handleResult(Object result) {
		GetGiftKardResponse response = (GetGiftKardResponse) result;
		if (response != null) {
			switch (response.getErrorCode()) {
				case BaseResponse.SUCCESS:
					List<ItemGiftKardRedeem> listGiftKards = response.getListGiftKards();
					if (listGiftKards != null && listGiftKards.size() > 0) {
						Message successMsg = new Message();
						successMsg.what = UIMessage.MSG_GET_DATA_SUCCESS;
						successMsg.obj = listGiftKards;
						mUIHandler.sendMessage(successMsg);
					}
					else {
						mUIHandler.sendEmptyMessage(UIMessage.MSG_INPUT_WRONG_CODE);
					}
					return true;
				case GetGiftKardResponse.ERROR_NOT_FOUND:
					mUIHandler.sendEmptyMessage(UIMessage.MSG_INPUT_WRONG_CODE);
					return true;
			}
		}
		return false;
	}

}
